package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geniuseoe2012.lazyloaderdemo.cache.ImageLoader;
import com.shoujifeng.snowmusic.ListView.XListViewFooter;
import com.shoujifeng.snowmusic.player.http.GlobalValue;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.LoadMask;

public class OnlineNewMusicMenuMoreActivity extends Activity {
	private GridView gridView;
	private ImageAdapter mAdapter;
	private final int UPDATE_DATA = 0;
	private ServerAccess serverAccess;
	private ArrayList<HashMap<String, Object>> mTmpList;
	private ArrayList<HashMap<String, Object>> mMusicList;
	private Thread accThread;
	private View imageView;
	private Handler mHandler;
	private int mPage = 0;
	private ProgressDialog pd;
	private RelativeLayout headView;
	private TextView headText;
	private ImageButton breakBtn;
	private LoadMask mLoadMask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.online_new_muisc_menu_more);
		init();
		setListView();
		initGlobalValue();
		mLoadMask = new LoadMask(this);
		mLoadMask.startLoad();
		accThread = new Thread(runnable);
		accThread.start();
	}

	private void init() {
		mHandler = new Handler();

		serverAccess = new ServerAccess(OnlineNewMusicMenuMoreActivity.this);
		gridView = (GridView) findViewById(R.id.gridview);
		imageView = LayoutInflater.from(OnlineNewMusicMenuMoreActivity.this)
				.inflate(R.layout.image_view, null);

		headView = (RelativeLayout) findViewById(R.id.headLay);
		headText = (TextView) headView.findViewById(R.id.head_text);
		breakBtn = (ImageButton) headView.findViewById(R.id.breakBtn);
		headText.setText("热门歌单");
		breakBtn.setOnClickListener(onClickListener);
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(OnlineNewMusicMenuMoreActivity.this,OnlineHotMenuContentActivity.class);
				String index = mMusicList.get(arg2).get(ServerAccess.ID).toString();
				String music = mMusicList.get(arg2).get(ServerAccess.MUSIC).toString();
				intent.putExtra(ServerAccess.MUSIC, music);
				intent.putExtra(ServerAccess.INDEX, index);
				startActivity(intent);
				
				overridePendingTransition(R.anim.right_in, R.anim.left_out);
			}
		});

	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.breakBtn:
				finish();
				
				overridePendingTransition(R.anim.no_move, R.anim.right_out);
			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}

	private void setListView() {
		mMusicList = new ArrayList<HashMap<String,Object>>();
		mAdapter = new ImageAdapter(OnlineNewMusicMenuMoreActivity.this, R.layout.online_new_music_menu_more_list);
		gridView.setAdapter(mAdapter);
	}



	private void initGlobalValue() {
		GlobalValue.initSDAddr();

		GlobalValue.addUrl(ServerAccess.GROUP_LIST,
				"http://www.snowmusic.com.cn/mobile_api/group_list.php");
	}

	protected Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case UPDATE_DATA: {
				mLoadMask.stopLoad();
				updateArrayList();
			}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	// 创建多线程获取数据
	protected Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
					String[] keys = new String[] {ServerAccess.ID ,ServerAccess.MUSIC,ServerAccess.LISTENER,ServerAccess.CODE,ServerAccess.ICON_URL};
					mTmpList = new ArrayList<HashMap<String, Object>>();
					mTmpList = serverAccess.getListMusicMenuMore(keys, mPage);
					sendMessage(UPDATE_DATA);

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	};

	// 发送消息到Handler
	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}

	// 若在线程中更新与Adapter的动态数组将会产生致命错误。
	private void updateArrayList() {

		if (mMusicList != null) {
			int count = mTmpList.size();
			for (int i = 0; i < count; i++) {
				mMusicList.add(mTmpList.get(i));
			}

			mTmpList.clear();
			mTmpList = null;
		}

		mAdapter.notifyDataSetChanged();
	}
	private class ImageAdapter extends BaseAdapter {
		private Context mContext;
		private int lay;
		private HashMap<String, Object> map;
		private ImageLoader mImageLoader;

		public ImageAdapter(Context context, int lay) {
			this.mContext = context;
			this.lay = lay;
			mImageLoader = new ImageLoader(context);
		}

		@Override
		public int getCount() {
			return mMusicList.size();
		}

		@Override
		public Object getItem(int position) {
			return mMusicList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 定义一个ImageView,显示在GridView里
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(lay, null);
				

			} 
				map = mMusicList.get(position);
				ViewHolder.imgBg = (ImageView) convertView
						.findViewById(R.id.lay4Img1);
				ViewHolder.mMusicName = (TextView) convertView
						.findViewById(R.id.lay4MusicName1);
				ViewHolder.mContent = (TextView) convertView
						.findViewById(R.id.lay4countTxt1);
				mImageLoader.DisplayImage(map.get(ServerAccess.ICON_URL)
						.toString(), ViewHolder.imgBg, false,
						R.drawable.icon_album_default);
				ViewHolder.mMusicName.setText(map.get(ServerAccess.MUSIC).toString());
				ViewHolder.mContent.setText(map.get(ServerAccess.LISTENER).toString());
			
			return convertView;
		}

	}

	static class ViewHolder {
		static ImageView imgBg;
		static TextView mMusicName;
		static TextView mContent;

	}

}
